package com.zyy.java8.samples.misc;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author zyy
 * @date 2022/4/11 下午10:13
 * @description
 */
public class Files1 {

    public static void main(String[] args) throws IOException {
        testWalk();
        testFind();
        testList();
        testLines();
        testReader();
        testWriter();
        testReadWriteLines();
        testReadLines();
    }

    private static void testWalk() throws IOException {
        Path start = Paths.get("");
        int maxDepth = 2;
        try (Stream<Path> pathStream = Files.walk(start, maxDepth)){
            String joined = pathStream
                    .map(String::valueOf)
                    .filter(path -> path.endsWith(".js"))
                    .collect(Collectors.joining("; "));
            System.out.println("walk(): = " + joined);
        }
    }

    private static void testFind() throws IOException {
        Path start = Paths.get("");
        int maxDepth = 2;
        try (Stream<Path> stream = Files.find(start, maxDepth, (path, attr)  ->
                String.valueOf(path).endsWith(".js"))){
            String joined = stream
                    .map(String::valueOf)
                    .sorted()
                    .collect(Collectors.joining("; "));
            System.out.println("find() = " + joined);
        }
    }

    private static void testList() throws IOException {
        Path start = Paths.get("");
        try (Stream<Path> stream = Files.list(start)){
            String joined = stream
                    .map(String::valueOf)
                    .filter(path -> !path.startsWith("."))
                    .sorted()
                    .collect(Collectors.joining("; "));
            System.out.println("list(); = " + joined);
        }
    }

    private static void testLines() throws IOException {
        Path start = Paths.get("res/nashorn1.js");
        try (Stream<String> stream = Files.lines(start)){
                stream
                        .filter(line -> line.contains("print"))
                        .map(String::trim)
                        .forEach(System.out::println);
        }
    }

    private static void testReader() throws IOException {
        Path start = Paths.get("res/nashorn1.js");
        try (BufferedReader reader = Files.newBufferedReader(start)){
            System.out.println(reader.readLine());
        }
    }

    private static void testWriter() throws IOException {
        Path outPut = Paths.get("res/outPut.js");
        try (BufferedWriter writer = Files.newBufferedWriter(outPut)){
            writer.write("print('hello,world')");
        }
    }

    private static void testReadWriteLines() throws IOException {
        Path readPath = Paths.get("res/nashorn1.js");
        List<String> lines = Files.readAllLines(readPath);
        lines.add("print('fooBar')");

        Path writePath = Paths.get("res", "nashorn1-modified.js");
        Files.write(writePath, lines);
    }

    private static void testReadLines() throws IOException {
        Path path = Paths.get("res/nashorn1.js");
        try (BufferedReader reader = Files.newBufferedReader(path)){
            long count = reader
                    .lines()
                    .filter(line -> line.contains("print"))
                    .count();
            System.out.println("count = " + count);
        }
    }

}
